<?php
require_once(sfConfig::get('sf_plugins_dir').'/sfDoctrineGuardPlugin/modules/sfGuardAuth/lib/BasesfGuardAuthActions.class.php');
 
class sfGuardAuthActions extends BasesfGuardAuthActions
{
  public function executeSignin($request)
  {
  	$languages = $this->getRequest()->getLanguages();
	$this->getUser()->setCulture($languages[0]);
  	/*if($request->isXmlHttpRequest()){
  		return sfView::NONE;
  	}*/
    $user = $this->getUser();
    if ($user->isAuthenticated())
    {
		return $this->redirect('@homepage');
    }
	$class = sfConfig::get('app_sf_guard_plugin_signin_form', 'sfGuardFormSignin');
	$this->form = new $class();

	if ($request->isMethod('post')){
		$request->setParameter('signin', array(
			'username' =>$_POST['user'],
			'password' =>$_POST['password'],
			'_csrf_token' => $_POST['csrf'],
			'remember' => $_POST['remember']
		  ));

		$this->form->bind($request->getParameter('signin'));
		if ($this->form->isValid())
		{
			$values = $this->form->getValues();
			$this->getUser()->signin($values['user']);
			$this->error = false;
			$this->message = time();
			$this->getResponse()->setCookie('userId', $this->getUser()->getId());
			$lang = $this->getUser()->getGuardUser()->getLanguage()->getLanguageCode();
			$this->getResponse()->setCookie('language', $lang);
			$this->getUser()->setCulture($lang);
			//return $this->redirect($request->getUri());
		  }
		  else
		  {
			$this->error = true;
			$this->message = $this->form->getErrorSchema();
			//$this->message = __("Username and/or password is invalid");
			//$message = $this->form->getErrorSchema();
		  }
	}
  }

  public function executeSignout($request)
  {
    $this->getUser()->signOut();

    $signoutUrl = sfConfig::get('app_sf_guard_plugin_success_signout_url', $request->getReferer());

    $this->redirect('' != $signoutUrl ? $signoutUrl : '@homepage');
  }
}
